package com.alogic.terminal.ssh.auth;

import ch.ethz.ssh2.Connection;
import com.alogic.terminal.ssh.Authenticator;
import com.anysoft.util.*;
import com.anysoft.util.code.Coder;
import com.anysoft.util.code.CoderFactory;
import com.anysoft.util.resource.ResourceFactory;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * 通过publicKey验证
 */
public class WithPublicKey implements Authenticator, Configurable {
    private static final Logger LOG = LoggerFactory.getLogger(WithPublicKey.class);
    private String username;
    private String password;
    private String keyContent;
    private Coder coder = null;

    private boolean isNullPassword(String pwd){
        return StringUtils.isEmpty(password) || "none".equals(password);
    }
    @Override
    public boolean authenticate(Connection conn) throws IOException {
        if (StringUtils.isEmpty(keyContent)) {
            return conn.authenticateWithPassword(username, password);
        }else{
            if (isNullPassword(password)) {
                return conn.authenticateWithPublicKey(username, keyContent.toCharArray(), null);
            }else{
                return conn.authenticateWithPublicKey(username, keyContent.toCharArray(), password);
            }
        }
    }

    @Override
    public void configure(Properties p) {
        username = PropertiesConstants.getString(p,"username","");
        password = PropertiesConstants.getString(p,"password","");

        if (!isNullPassword(password)) {
            try {
                Coder coder = CoderFactory.newCoder(PropertiesConstants.getString(p, "coder", "DES3"));
                password = coder.decode(password, username);
            } catch (Exception ex) {
                LOG.error("Can not find coder:" + coder);
            }
        }

        keyContent = PropertiesConstants.getString(p,"keyContent","");
        if (StringUtils.isEmpty(keyContent)){
            String keyPath = PropertiesConstants.getString(p,"keyPath","");
            keyContent = readKeyContent(keyPath,"");
        }
    }

    protected static String readKeyContent(String keyPath,String dft) {
        ResourceFactory rf = Settings.getResourceFactory();
        InputStream in = null;
        try {
             in = rf.load(keyPath, null);
            BufferedReader reader = new BufferedReader(new InputStreamReader(in));
            StringBuffer content = new StringBuffer();
            String line = null;
            while ((line = reader.readLine()) != null) {
                content.append(line).append("\n");
            }
            return content.toString();
        }catch (Exception ex){
            LOG.error("Can not read content from file: " + keyPath);
            LOG.error(ex.getMessage());
            return dft;
        }finally{
            IOTools.close(in);
        }
    }
}
